package com.wizz.trade.trading.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.entity.Currency;
import com.wizz.trade.information.entity.Variety;
import com.wizz.trade.information.mapper.*;
import com.wizz.trade.trading.dto.FpositionDto;
import com.wizz.trade.trading.dto.page.FpositionPageDto;
import com.wizz.trade.trading.entity.Fposition;
import com.wizz.trade.trading.entity.Transaction;
import com.wizz.trade.trading.mapper.FpositionMapper;
import com.wizz.trade.trading.mapper.TransactionMapper;
import com.wizz.trade.trading.service.FpositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wizz.trade.trading.vo.FpositionVo;
import com.wizz.trade.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 期货头寸表 服务实现类
 * </p>
 *
 * @author fyftless
 * @since 2023-08-28 06:17:59
 */
@Service
public class FpositionServiceImpl extends ServiceImpl<FpositionMapper, Fposition> implements FpositionService {

    @Resource
    private FpositionMapper fpositionMapper;

    @Resource
    private TransactionMapper transactionMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private CompanydeptMapper companydeptMapper;

    @Resource
    private VarietyMapper varietyMapper;

    @Resource
    private GradeMapper gradeMapper;

    @Resource
    private CurrencyMapper currencyMapper;

    @Override
    public void addFposition(FpositionDto fpositionDto) {
        Fposition fposition = new Fposition();
        BeanUtils.copyProperties(fpositionDto, fposition);

        // 计算期货浮动盈亏
        Transaction transaction = transactionMapper.selectById(fpositionDto.getTransaction());
        BigDecimal weighting = transaction.getWeighting();
        Integer perhands = transaction.getPerhands();
        fposition.setFpl(fpositionDto.getFsprice().subtract(weighting)
                .multiply(BigDecimal.valueOf((long)perhands * fpositionDto.getPoshands())));

        fpositionMapper.insert(fposition);
    }

    @Override
    public void updateFposition(Fposition fposition) {
        fpositionMapper.updateById(fposition);
    }

    @Override
    public void deleteFposition(Integer id) {
        fpositionMapper.deleteById(id);
    }

    @Override
    public PageVo<FpositionVo> getPage(FpositionPageDto fpositionPageDto) {
        LambdaQueryWrapper<Fposition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(fpositionPageDto.getHedgetype()), Fposition::getHedgetype, fpositionPageDto.getHedgetype())
                .eq(Objects.nonNull(fpositionPageDto.getLedger()), Fposition::getLedger, fpositionPageDto.getLedger())
                .eq(Objects.nonNull(fpositionPageDto.getOurdept()), Fposition::getOurdept, fpositionPageDto.getOurdept())
                .eq(Objects.nonNull(fpositionPageDto.getFutures()), Fposition::getFutures, fpositionPageDto.getFutures())
                .eq(StringUtils.isNotBlank(fpositionPageDto.getContract()), Fposition::getContract, fpositionPageDto.getContract())
                .eq(Objects.nonNull(fpositionPageDto.getVariety()), Fposition::getVariety, fpositionPageDto.getVariety())
                .eq(Objects.nonNull(fpositionPageDto.getGrade()), Fposition::getGrade, fpositionPageDto.getGrade())
                .eq(StringUtils.isNotBlank(fpositionPageDto.getPositionno()), Fposition::getPositionno, fpositionPageDto.getPositionno());
        Page<Fposition> page = fpositionMapper.selectPage(PageUtil.initPage(fpositionPageDto), queryWrapper);
        List<Fposition> records = page.getRecords();
        List<FpositionVo> result = new ArrayList<>();
        for (Fposition record : records) {
            FpositionVo vo = new FpositionVo();
            Transaction transaction = transactionMapper.selectById(record.getTransaction());
            BeanUtils.copyProperties(record, vo);
            vo.setDate(transaction.getDate());
            vo.setLedger(companyMapper.selectById(record.getLedger()).getShortname());
            vo.setOurdept(companydeptMapper.selectById(record.getOurdept()).getName());
            vo.setFutures(companyMapper.selectById(record.getFutures()).getShortname());
            Variety variety = varietyMapper.selectById(record.getVariety());
            vo.setVariety(variety.getName());
            vo.setGrade(gradeMapper.selectById(record.getGrade()).getName());
            vo.setUnit(variety.getUnit());
            vo.setCurrency(currencyMapper.selectById(transaction.getCurrency()).getName());
            vo.setBs(transaction.getBs());
            vo.setHands(transaction.getHands());
            vo.setPerhands(transaction.getPerhands());
            vo.setWeighting(transaction.getWeighting());
            result.add(vo);
        }
        return new PageVo<>(page.getPages(), page.getTotal(), result);
    }

}
